package com.wrench.elevator.base.user.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wrench.elevator.base.user.dao.PersonDao;
import com.wrench.elevator.base.user.domain.Person;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

/**
 * @Description:PersonService
 * @author yangchenfeng
 * @date Oct 25, 2017 5:37:33 PM
 * @version V1.0
 */
@Component
public class PersonService
{
    @Resource
    private PersonDao dao;
    
    private static final Logger logger = LoggerFactory.getLogger(PersonService.class);
    
    /**
     * 给该用户添加人（脸）person_id
     * */
    public int addPerson(String user_id, String person_id, String identity, String input_time)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(user_id, person_id, identity, input_time))
        {
            logger.error("==> param is not found , user_id = {} ,person_id = {} ,identity = {} , input_time = {} ",
                user_id,
                person_id,
                identity,
                input_time);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        user_id = user_id.trim();
        person_id = person_id.trim();
        identity = identity.trim();
        input_time = input_time.trim();
        
        if (!WrenchUtil.isInt(user_id))
        {
            logger.error("==> string is not int , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
            
        }
        
        if (!WrenchUtil.isVaildIdentity(identity))
        {
            logger.error("==> identity is not vaild , identity = {} ", identity);
            throw new WrenchException(Result.PERSON_IDENTITY_IS_NOT_VAILD_CODE,
                Result.PERSON_IDENTITY_IS_NOT_VAILD_MESSAGE);
        }
        
        if (!WrenchUtil.isVaildTime(input_time))
        {
            logger.error("==> time is not vaild , input_time = {} ", input_time);
            throw new WrenchException(Result.TIME_IS_NOT_VAILD_CODE, Result.TIME_IS_NOT_VAILD_MESSAGE);
        }
        
        Person person = new Person();
        person.setIdentity(identity);
        person.setInput_time(input_time);
        person.setPerson_id(person_id);
        person.setUser_id(Integer.valueOf(user_id));
        
        int commit = -1;
        try
        {
            commit = dao.addPerson(person);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==>  commit=dao.addPerson(person) execute error , person = {} , error = {}; ", person, e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.addPerson(person) execute success , but commit <=0 , commit = {} , person = {} ",
                commit,
                person);
            throw new WrenchException(Result.USER_REG_BY_TELPHONE_FAIL_CODE, Result.USER_REG_BY_TELPHONE_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.addPerson(person) execute success , commit = {} , person = {} ", commit, person);
        
        return commit;
    }
    
    /***
     * 根据User_id获取person_id
     * 
     * @throws WrenchException
     * */
    public List<String> getPersonId(String user_id)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(user_id))
        {
            logger.error("==> param is not found , user_id = {} ", user_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchUtil.isInt(user_id))
        {
            logger.error("==> string is not int , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        List<String> person_id = null;
        try
        {
            person_id = dao.getPersonId(Integer.valueOf(user_id));
        }
        catch (Exception e)
        {
            logger.error("==> person_id=dao.getPersonId(Integer.valueOf(user_id)) execute error ,user_id = {} , error = {}; ",
                user_id,
                e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        logger.debug("==> person_id=dao.getPersonId(Integer.valueOf(user_id)) execute success , user_id = {} , person_id = {}",
            user_id,
            person_id);
        return person_id;
    }
    
    /**
     * 根据Person_id 获取person
     * 
     * @throws WrenchException
     * */
    public Person getPerson(String person_id)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(person_id))
        {
            logger.error("==> param is not found , person_id = {} ", person_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        Person person = null;
        
        try
        {
            person = dao.getPerson(person_id);
        }
        catch (Exception e)
        {
            logger.error("==> person=dao.getPerson(person_id) execute error ,person_id = {} , error = {}; ",
                person_id,
                e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        logger.debug("==> person=dao.getPerson(person_id) execute success , person = {} , person_id = {}",
            person,
            person_id);
        return person;
        
    }
    
}
